package cn.px.rest.controller.customize;

import cn.px.common.util.R;
import cn.px.dao.dto.customize.CustomizeReportDTO;
import cn.px.dao.entity.customize.CustomizeReport;
import cn.px.service.customize.CustomizeReportService;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.customize.ExportCustomizeReportVO;
import cn.px.dao.vo.query.customize.CustomizeReportVO;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.rest.log.annotation.SysLog;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 自定义报表 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/customize/reports")
public class CustomizeReportController {

  private final  CustomizeReportService customizeReportService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<CustomizeReport> getById(@PathVariable("id") String id){
      return new R<>(customizeReportService.getModelById(id));
  }

  /**
   * 新增记录
   *
   * @param customizeReportDTO 自定义报表DTO
   * @return R
   */
  @SysLog("新增自定义报表")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('customize-reports-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody CustomizeReportDTO customizeReportDTO) {
      return new R<>(customizeReportService.saveModelByDTO(customizeReportDTO));
  }

  /**
   * 修改记录
   *
   * @param customizeReportDTO 自定义报表DTO
   * @return R
   */
  @SysLog("修改自定义报表")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('customize-reports-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody CustomizeReportDTO customizeReportDTO) {
      customizeReportService.modModelByDTO(customizeReportDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除自定义报表")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('customize-reports-del')")
  public R removeById(@PathVariable String id){
    customizeReportService.deleteByPrimaryKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除自定义报表")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('customize-reports-del')")
    public R removeIds(@RequestBody List<String> ids){
        customizeReportService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param customizeReportDTO 自定义报表DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<CustomizeReportVO>> query(CustomizeReportDTO customizeReportDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(customizeReportService.selectPageList(customizeReportDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param customizeReportDTO 自定义报表DTO
   * @return R
   */
  @GetMapping("")
  public R query(CustomizeReportDTO customizeReportDTO) {
      return new R<>(customizeReportService.getModelListByDTO(customizeReportDTO));
  }

  /**
     * 自定义报表导出
     *
     * @param customizeReportDTO 自定义报表DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("自定义报表导出")
  @PreAuthorize("@pms.hasPermission('customize-reports-export')")
  public void export(HttpServletResponse response, CustomizeReportDTO customizeReportDTO) {
    List<CustomizeReport> list = customizeReportService.getModelListByDTO(customizeReportDTO);
    ExcelUtil.exportResponseDict(response, ExportCustomizeReportVO.class, BeanConverterUtil.copyListProperties(list, ExportCustomizeReportVO.class), "自定义报表");
  }

}
